

// 服务相关信息
const env = "test"
let AiHost = ""
let apiFeedbackPoint = ""
if (env === "test") {
    AiHost = "http://192.168.10.211:9080/ai"
    apiFeedbackPoint = "http://localhost:7777/api/addInfoPoint"
} else {
    AiHost = "http://gateway.lsmedia.com/ai"
    apiFeedbackPoint = "http://gateway.lsmedia.com/okr/api/addInfoPoint"
}


const isTest = true
const testToken = "c94497e248412c59d8558c75679e26ac"

const apiChat = AiHost + "/chat/chat"
// const apiChat = "http://192.168.10.118:9652/chat"


const apiAgent = AiHost + "/agent/list"
const apiEventList = AiHost + "/event/list"
const apiEventPrompt = AiHost + "/event/solvePrompt"
const apiCategoryList = AiHost + "/question/categoryList"
const apiQuestionList = AiHost + "/question/questionList"
const apiFeedbackQuestionList = AiHost + "/feedback/questionList"
const apiFeedbackSystemList = AiHost + "/event/systemList"
const apiFeedbackSubmit = AiHost + "/feedback/feedback"
const apiChatCallback = AiHost + "/chat/callback"

let userInfo = {} // 当天会话信息
// 检查登录状态
if (typeof chrome !== 'undefined' && typeof chrome.runtime !== 'undefined') {
    checkLogin()
}
// 检查登录
function checkLogin() {
    userInfo = localStorage.getItem("userInfo")
    if (userInfo === null) {
        console.log("未登录")
        chrome.sidePanel.setOptions({ path: "sidebar/login.html", });
    }
    console.log("已登录")
    userInfo = JSON.parse(userInfo)
    console.log(userInfo)
}

async function requestGet(url) {
    const token = getUserToken();
    try {
        const response = await fetch(url, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'token': token
            },
        });

        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }

        const data = await response.json();
        if (data.code === 402) {
            console.log("登录掉线");
            chrome.sidePanel.setOptions({ path: "sidebar/login.html" });
        }
        return data;
    } catch (error) {
        console.error('请求出错:', error);
    }
}

async function requestPost(url, postData) {
    let token = getUserToken()
    try {
        const response = await fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'token': token
            },
            body: JSON.stringify(postData),
        });

        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }

        const data = await response.json();
        return data;
    } catch (error) {
        console.error('请求出错:', error);
    }
}

async function requestChart(url, msgBody, signal, callback) {
    const token = getUserToken();
    try {
        const response = await fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'token': token
            },
            body: JSON.stringify(msgBody),
            signal: signal, // 中断请求
        });
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        console.log("response", response);
        const reader = response.body.getReader();
        callback({ status: response.status, reader });
    } catch (error) {
        if (error.name === 'AbortError') {
            console.log('请求已被主动中断');
            // $(".loading").remove();
            // $(".loading").css("display", "none");
            // $("#stop-signal-btn").attr("disabled", true)

            // sseDoneHandler()

        } else {
            console.error('请求出错:', error);
        }
        callback({ status: 500, error: error });
    }
}

function getUserToken() {
    if (isTest === true) {
        return testToken
    }
    return userInfo.token
}